<?php
class B2b_GalleryController extends Zend_Controller_Action {

    /**
    * List album of enterprise
    * @return
    **/
    public function listAction()
    {
        $request = $this->getRequest();
        $lang = $request->getParam('lang');
        $enterprise_id = $request->getParam('enterprise_id');
        
    	/**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
			/*
				Name: Cong Ngo , Date: 11:44 29/4/2011
				Purpose	: tránh cho ng dùng login với tài khoản này lại sửa được info của tài khoản khác
			*/
				/*
			$redirectFlag = false;
            if($user->enterprise != null){
				$enterprise_id_edit = $this->getRequest()->getParam("enterprise_id");			
				if($user->enterprise->id != $enterprise_id_edit)
					$redirectFlag = true;
			}else $redirectFlag = true;
			if($redirectFlag){
				$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
				$redirector->gotoUrl('');								
			}		
				*/
			/* End Edit */			
        	if($user->enterprise){
                $user_id = $user->user_id;
            }
            $this->view->assign('user', $user);
        }
        
    	/**
         * Check allow action
        **/
        $allowAction = ($enterprise_id != @$user->enterprise->id)?false:true;
        $this->view->assign('allowAction', $allowAction);

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Enterprise basic model access to db
         */
        $galleryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getGalleryDao();
        $galleryDao->setDbConnection($conn);

        /**
         * Enterprise basic model access to db
         */
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);
        
    	
		/** Khanh *****************************************************************************************************
	    * Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
	    * $user_entity = 1 : User chua login
	    * $user_entity = 2 : User da login, nhung ko la DN
	    * $user_entity = 3 : User da login, la DN khach
	    * $user_entity = 4 : User da login, la DN doi tac
	    **/
        
        $params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
		$permissionDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPermissionDao();
		$permissionDao->setDbConnection($conn);
        //$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) 
		{ 
		    $user = $auth->getIdentity();
		    if(!isset($user->enterprise->id))
		    {
		      	$user_entity = 2; // user da login, nhung ko phai la DN
		    }
			else
			{
			   	$partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
		        $partnerDao->setDbConnection($conn);
		
		        $pageIndex     = 1;
		        $perPage = 5;
		        $offset     = ($pageIndex - 1) * $perPage;
		        $exp = array(
		            'relationid' => $params['enterprise_id'],
		            'partner_type' => 'buddy',
		            'partner_status' => 'yes',
					'deleted'		 => 0
		        );
						
		        //* In ralationship
		        $partners = $partnerDao->find($offset, $perPage, $exp);
		        $numPartners   = $partnerDao->count($exp);
		             
		        $user_entity = 3;// DN khach
		        $request = $this->getRequest();
		        foreach($partners as $row):
					$expRecomment = array(
					'relationid' => $row->relationid,
					'enterprise_id' => $row->enterprise_id, // DN doi tac
					);
					if($user->enterprise->id == $row->enterprise_id)
					{
						$user_entity = 4; // DN doi tac
					}
				endforeach;
			}
		}
		else 
		{
		   	$user_entity = 1; // user chua login 
		}
			
		$this->view->assign('user_entity', $user_entity);
			
		$permission = $permissionDao->getPermissionByUserEntity($enterprise_id, $user_entity);
		if($permission != null){
			$permission->enterprise_info = unserialize($permission->enterprise_info);			
			$this->view->assign('permission', $permission);
		}
    	if ($auth->hasIdentity()) { 
	            $user = $auth->getIdentity();
				if($user->enterprise){
					if($user->enterprise->id == $enterprise_id) // user đã login thuộc DN đang xem
			    	{
			    		$permission_viewall = 1;
			    		$this->view->assign('permission_viewall', $permission_viewall);
			    	}
				}
        	}
		/**
		* END : Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
		*****************************************************************************************************/
		
		if(isset($params['enterprise_id']))
		{
			//echo $user->enterprise->id . "-".$params['enterprise_id'];
			//$auth = Zend_Auth::getInstance();
		    if ($auth->hasIdentity()) 
		    { 
		        $user = $auth->getIdentity();
		        if(!isset($user->enterprise->id)){
			      	$user_entity = 2; // user da login, nhung ko phai la DN
		        }
			    else
			    {
			      	$partnerDao = Tomato_Model_Dao_Factory::getInstance()->setModule('bizprofile')->getPartnerDao();
		            $partnerDao->setDbConnection($conn);
		
		            $pageIndex     = 1;
		            $perPage = 5;
		            $offset     = ($pageIndex - 1) * $perPage;
		            $exp = array(
		                'relationid' => $params['enterprise_id'],
		                'partner_type' => 'buddy',
		                'partner_status' => 'yes',
						'deleted'		 => 0
		            );
						
		            //* In ralationship
		            $partners = $partnerDao->find($offset, $perPage, $exp);
		            $numPartners   = $partnerDao->count($exp);
		               
		            $user_entity = 3;// DN khach
		            $request = $this->getRequest();
		            foreach($partners as $row):
						$expRecomment = array(
						'relationid' => $row->relationid,
						'enterprise_id' => $row->enterprise_id, // DN doi tac
						);
						if($user->enterprise->id == $row->enterprise_id)
						{
							$user_entity = 4; // DN doi tac
						}
					endforeach;
			       }
		    }
		    else 
		    {
		      	$user_entity = 1; // user chua login 
		    }
		}
			
		else 
		{
			$user_entity = 0;
			//$user_entity = $permissionDao->getUserEntity($user_id,$enterprise_id);
		}
		/*if($user->enterprise->id == $params['enterprise_id'])
		{
			$user_entity = 0;
		}*/
        $this->view->assign('user_entity', $user_entity);
		$permission = $permissionDao->getPermissionByUserEntity($enterprise_id, $user_entity);
        if($permission != null){
            $permission->enterprise_info = unserialize($permission->enterprise_info);
            $this->view->assign('permission', $permission);
        }
		/**
		* END : Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
		**/
		
        $this->view->assign('user_entity', $user_entity);
		$permission = $permissionDao->getPermissionByUserEntity($enterprise_id, $user_entity);
        if($permission != null){
            $permission->enterprise_info = unserialize($permission->enterprise_info);
            $this->view->assign('permission', $permission);
        }
		/**
		* END : Kiem tra la user chua login, hay user da login, la DN khach, hay DN doi tac
		**************************************************************************************/ 
          
        /**
        * Condition chain
        **/
        $pageIndex = $request->getParam('pageIndex', 1);
        $perPage   = 20;
        $offset    = ($pageIndex - 1) * $perPage;

        /**
         * Get set for selectbox
        **/
        $sets = $galleryDao->getSetInfo($enterprise_id, $offset, $perPage);
        $total = $galleryDao->count($enterprise_id);

        /**
         * Get current enterprise
        **/
        if($enterprise_id){
            $objEnterprise = $enterpriseDao->getById($enterprise_id);
        }
        $found = true;
        if (null == $objEnterprise) {
            $found = false;
        }
        $this->view->assign('found', $found);
        $this->view->assign('enterprise', $objEnterprise);

        /**
        * Paginator
        */
        $paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($sets, $total));
        $paginator->setCurrentPageNumber($pageIndex);
        $paginator->setItemCountPerPage($perPage);

        $this->view->assign('pageIndex', $pageIndex);
        $this->view->assign('sets', $sets);
        $this->view->assign('numGallery', $total);

        $this->view->assign('paginator', $paginator);

        $arrParamGallery = array(
            'enterprise_id' => $objEnterprise->id,
            'enterprise_name' => Tomato_Utility_String::removeSign($objEnterprise->company_name_en, '-', true)
        );
        $this->view->assign('paginatorOptions', array(
            'path'        => $this->view->url($arrParamGallery, 'b2b_gallery_list'),
            'itemLink' => 'page-%d',
        ));
    }

    public function addAction(){
        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
			$redirectFlag = false;
            if($user->enterprise != null){
				$enterprise_id_edit = $this->getRequest()->getParam("enterprise_id");			
				if($user->enterprise->id != $enterprise_id_edit)
					$redirectFlag = true;
			}else $redirectFlag = true;		
			if($redirectFlag){
				$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
				$redirector->gotoUrl('');								
			}						
            $this->view->assign('user', $user);
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        /**
         * Check user enterprise exist
        **/
        if(!isset($user->enterprise->id)){
            die('Not foud enterprise');
        }

        $request = $this->getRequest();
        $lang = $request->getParam('lang');

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Enterprise basic model access to db
         */
        $galleryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getGalleryDao();
        $galleryDao->setDbConnection($conn);

        /**
         * Blog model access to db
         */
        $blogDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getBlogDao();
        $blogDao->setDbConnection($conn);

        /**
         * Enterprise basic model access to db
         */
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);

        /**
         * Media set access to data
         **/
        $setDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getSetDao();
        $setDao->setDbConnection($conn);

        /**
         * Media file access to data
         **/
        $fileDao = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getFileDao();
        $fileDao->setDbConnection($conn);

        /**
         * Get current enterprise
        **/
        $objEnterprise = $enterpriseDao->getById($user->enterprise->id);
        $this->view->assign('enterprise', $objEnterprise);

        /**
         * Get set for selectbox
        **/
        $sets = $galleryDao->getSetInfo($user->enterprise->id);
        $this->view->assign('sets', $sets);

        if($request->isPost()){

            $title = $request->getPost('title');

            if ($title && $title != ',') {
                $square    = $request->getPost('square');
                $thumbnail = $request->getPost('thumbnail');
                $small        = $request->getPost('small');
                $crop        = $request->getPost('crop');
                $medium    = $request->getPost('medium');
                $large        = $request->getPost('large');
                $original  = $request->getPost('original');

                $setId        = $request->getPost('oldSet');
                $newSet    = $request->getPost('newSet');

                $arrTitle     = explode(',', $title);
                $arrCrop     = explode(',', $crop);
                $arrThumbnail = explode(',', $thumbnail);
                $arrLarge     = explode(',', $large);
                $arrMedium     = explode(',', $medium);
                $arrOrgin     = explode(',', $original);
                $arrSmall     = explode(',', $small);
                $arrSquare     = explode(',', $square);
                $isAdd         = false;

                /**
                * Tạo biến lưu nội dung bài viết và đạc giá trị default
                **/
                $image_content = null;

                for ($i = 1; $i < count($arrTitle); $i++){
                    $arrImageTitle = explode('.', $arrTitle[$i]);
                    $imageName = substr($arrTitle[$i], 0, strlen($arrTitle[$i]) - strlen($arrImageTitle[count($arrImageTitle)-1]) - 1);
                    $file = new Multimedia_Models_File(array(
                        'title'             => $imageName,
                        'slug'                 => Tomato_Utility_String::removeSign($imageName, '-', true),
                        'image_square'         => $arrSquare[$i],
                        'image_thumbnail'     => $arrThumbnail[$i],
                        'image_small'         => $arrSmall[$i],
                        'image_crop'         => $arrCrop[$i],
                        'image_medium'         => $arrMedium[$i],
                        'image_large'         => $arrLarge[$i],
                        'image_original'     => $arrOrgin[$i],
                        'created_date'         => date('Y-m-d H:i:s'),
                        'created_user'         => $user->user_id,
                        'created_user_name' => $user->user_name,
                        'url'                 => $arrOrgin[$i],
                        'file_type'         => 'image',
                        'is_active'         => true,
                    ));
                    $fileId = $fileDao->add($file);

                    if ($newSet && !$isAdd) {
                        $isAdd = true;
                        $set = new Multimedia_Models_Set(array(
                            'title'             => $newSet,
                            'slug'              => Tomato_Utility_String::removeSign($newSet, '-', true),
                            'created_date'      => date('Y-m-d H:i:s'),
                            'created_user_id'   => $user->user_id,
                            'created_user_name' => $user->user_name,
                            'is_active'         => true,
                        ));
                        $setId = $setDao->add($set);
                        if($setId){

                            $gallery = new B2b_Models_Gallery(array(
                                'enterprise_id' => $user->enterprise->id,
                                'set_id'        => $setId,
                                'num_image'     => count($arrTitle)-1,
                            ));
                            $galleryDao->add($gallery);
                        }
                    }
                    if ($setId) {
                        $fileDao->addToSet($setId, $fileId);
                    }

                    /**
                     * Update for set
                    **/
                    if ($setId){
                        if($i == (count($arrTitle)-1)){
                            /**
                            * Update representative image for set
                            **/
                            $updateSet = new Multimedia_Models_Set(array(
                                'set_id'          => $setId,
                                'image_square'    => $arrSquare[$i],
                                'image_thumbnail' => $arrThumbnail[$i],
                                'image_small'     => $arrSmall[$i],
                                'image_crop'      => $arrCrop[$i],
                                'image_medium'    => $arrMedium[$i],
                                'image_large'     => $arrLarge[$i],
                            ));
                            $galleryDao->updateSet($updateSet);

                            if(!$isAdd){
                                /**
                                * Update num image for set
                                **/
                                $galleryDao->updateNImage($setId, count($arrTitle)-1);
                            }
                        }
                    }

                    /**
                     * Tạo nội hình ảnh cho bài viết
                    **/
                    $arrRouteDetail = array(
                        'enterprise_id' => $objEnterprise->id,
                        'enterprise_name' => Tomato_Utility_String::removeSign($objEnterprise->company_name_en, '-', true),
                        'slug' => Tomato_Utility_String::removeSign($newSet, '-', true),
                        'set_id' => $setId,
                        'file_id' => $fileId,
                        'pageIndex' => $i,
                        'image_slug' => Tomato_Utility_String::removeSign($imageName, '-', true)
                    );
                    $image_content .= "<a href='{$this->view->url($arrRouteDetail, 'b2b_gallery_detail')}'><img width='120' src='{$arrThumbnail[$i]}'/></a>&nbsp;&nbsp;";
                }

                $blog_id       = null;
                $link          = null;
                $content       = $image_content;
                $date          = date('Y-m-d');
                $enterprise_id = $objEnterprise->id;

                $blog = new B2b_Models_Blog(array(
                        'link'          => $link,
                        'content'       => $content,
                        'date'          => $date,
                        'enterprise_id' => $enterprise_id,
                ));
                $arrLang = array('vi_VN', 'en_US');
                foreach($arrLang as $r_lang){
                    if($r_lang == 'vi_VN'){
                        $title = "Ảnh mới cập nhật";
                    }else{
                        $title = "Gallery updated";
                    }
                    $blog->title = $title;
                    $blog->slug = Tomato_Utility_String::removeSign($title, '-', true);

                    $blog->language = $r_lang;
                    $blogDao->add($blog);
                }

                $this->_helper->getHelper('FlashMessenger')
                    ->addMessage($this->view->translator('gallery_upload_success'));
                $arrRouteAdd = array(
                    'enterprise_id' => $objEnterprise->id,
                    'enterprise_name' => Tomato_Utility_String::removeSign($objEnterprise->company_name_en, '-', true),
                );
                $this->_redirect($this->view->serverUrl() . $this->view->url($arrRouteAdd, 'b2b_gallery_add'));
            }
        }
    }

    public function albumAction(){
    	$request = $this->getRequest();
        $set_id = $request->getParam('set_id');
        $enterprise_id = $request->getParam('enterprise_id');
        
    	/**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
            $allow_mark = 1;
        }
        else 
        {
        	$allow_mark = 0; // chưa login, ko cho hiển thị chức năng mark ảnh
        }
        
        /**
         * Check user enterprise exist
        **/
        if(!isset($user->enterprise->id)){
        	$allow_mark = 0;
            //die('Not foud enterprise');
        }
        else 
        {
        	if($enterprise_id==$user->enterprise->id)
        		$allow_mark = 1;
        	else 
        		$allow_mark = 0;
        }
        
        
        

        $request       = $this->getRequest();
        $lang          = $request->getParam('lang');
        $enterprise_id = $request->getParam('enterprise_id');
        $set_id        = $request->getParam('set_id');

        /**
         * Check allow action
        **/
        $allowAction = ($enterprise_id != @$user->enterprise->id)?false:true;
        $this->view->assign('allowAction', $allowAction);

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Enterprise basic model access to db
         */
        $galleryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getGalleryDao();
        $galleryDao->setDbConnection($conn);
        
        

        /**
         * Enterprise basic model access to db
         */
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);

        /**
         * Media set access to data
         **/
        $setDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getSetDao();
        $setDao->setDbConnection($conn);

        /**
         * Media file access to data
         **/
        $fileDao = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getFileDao();
        $fileDao->setDbConnection($conn);

        /**
         * Get current enterprise
        **/
        $objEnterprise = $enterpriseDao->getById($enterprise_id);
        $found = true;
        if (null == $objEnterprise) {
            $found = false;
        }
        $this->view->assign('found', $found);
        $this->view->assign('enterprise', $objEnterprise);

        $set = new Multimedia_Models_Set(array());
        $photos = new Multimedia_Models_File(array());
        
        
        
        $total = 0;
        if($set_id){
            /**
            * Get set info
            **/
            $set = $setDao->getById($set_id);

            /**
            * Get image in set
            **/
            $pageIndex = $request->getParam('pageIndex', 1);
            $perPage   = 25;
            $offset    = ($pageIndex - 1) * $perPage;
            $this->view->assign('offset', $offset);

            $photos = $fileDao->getFilesInSet($set_id, $offset, $perPage, 1);
            $total = $fileDao->countFilesInSet($set_id, 1);
            /**
            * Paginator
            */
            $paginator = new Zend_Paginator(new Tomato_Utility_PaginatorAdapter($photos, $total));
            $paginator->setCurrentPageNumber($pageIndex);
            $paginator->setItemCountPerPage($perPage);

            $this->view->assign('paginator', $paginator);

            $arrRouteAlbum = array(
                'enterprise_id' => $objEnterprise->id,
                'enterprise_name' => Tomato_Utility_String::removeSign($objEnterprise->company_name_en, '-', true),
                'slug' => $set->slug,
                'set_id' => $set->set_id
            );
            $this->view->assign('paginatorOptions', array(
                'path'        => $this->view->url($arrRouteAlbum, 'b2b_gallery_album'),
                'itemLink' => 'page-%d',
            ));
        }
        
        // Mark image
        $arrListFileId = $setDao->getListFileId($set_id);
        $this->view->assign('arrListFileId', $arrListFileId);
        for($i=0; $i<count($arrListFileId); $i++)
        {
        	$checkmark[$i] = $galleryDao->checkmark($arrListFileId[$i]->file_id);
        	
        	$this->view->assign("checkmark", $checkmark);
        }
        // End Mark image
        
        $this->view->assign('set', $set);
        $this->view->assign('pageIndex', $pageIndex);
        $this->view->assign('photos', $photos);
        $this->view->assign('numPhoto', $total);
        $this->view->assign('allow_mark', $allow_mark);
        
    }

    public function detailAction(){

        $request       = $this->getRequest();
        
        $lang          = $request->getParam('lang');
        $enterprise_id = $request->getParam('enterprise_id');
        $set_id        = $request->getParam('set_id');
        $file_id        = $request->getParam('file_id');
        
    	/**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
            $allow_mark = 1;
        }
        else 
        {
        	$allow_mark = 0; // chưa login, ko cho hiển thị chức năng mark ảnh
        }
        
        /**
         * Check user enterprise exist
        **/
        if(!isset($user->enterprise->id)){
        	$allow_mark = 0;
            //die('Not foud enterprise');
        }
        else 
        {
        	if($enterprise_id==$user->enterprise->id)
        		$allow_mark = 1;
        	else 
        		$allow_mark = 0;
        }

        

        /**
         * Check allow action
        **/
        $allowAction = ($enterprise_id != @$user->enterprise->id)?false:true;
        $this->view->assign('allowAction', $allowAction);

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Enterprise basic model access to db
         */
        $galleryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getGalleryDao();
        $galleryDao->setDbConnection($conn);

        /**
         * Enterprise basic model access to db
         */
        $enterpriseDao = Tomato_Model_Dao_Factory::getInstance()->setModule('enterprise')->getEnterpriseDao();
        $enterpriseDao->setDbConnection($conn);

        /**
         * Media set access to data
         **/
        $setDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getSetDao();
        $setDao->setDbConnection($conn);

        /**
         * Media file access to data
         **/
        $fileDao = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getFileDao();
        $fileDao->setDbConnection($conn);

        /**
         * Get current enterprise
        **/
        $objEnterprise = $enterpriseDao->getById($enterprise_id);
        $found = true;
        if (null == $objEnterprise) {
            $found = false;
        }
        $this->view->assign('found', $found);
        $this->view->assign('enterprise', $objEnterprise);

        $set = new Multimedia_Models_Set(array());
        $photos = new Multimedia_Models_File(array());
        $total = 0;
        if($set_id){
            /**
            * Get set info
            **/
            $set = $setDao->getById($set_id);

            /**
            * Get image in set
            **/
            $pageIndex = $request->getParam('pageIndex', 1);
            $perPage   = 1    ;
            $offset    = ($pageIndex - 1) * $perPage;

            $photos = $fileDao->getFilesInSet($set_id, $offset, $perPage, 1);
            $total = $fileDao->countFilesInSet($set_id, 1);
        }
        
        // Mark image
        $checkmark = $galleryDao->checkmark($file_id);
        $this->view->assign('checkmark', $checkmark);
        // End Mark image
        
        $this->view->assign('set', $set);
        $this->view->assign('pageIndex', $pageIndex);
        $this->view->assign('photos', $photos);
        $this->view->assign('numPhoto', $total);
        $this->view->assign('allow_mark', $allow_mark);
    }

    public function deleteFileAction(){

        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        /**
         * Check user enterprise exist
        **/
        if(!isset($user->enterprise->id)){
            die('Not foud enterprise');
        }

        $request = $this->getRequest();
        $set_id = $request->getParam('set_id');
        $file_id = $request->getParam('file_id');
        $enterprise_id = $request->getParam('enterprise_id');

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
         * Media file access to data
         **/
        $fileDao = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getFileDao();
        $fileDao->setDbConnection($conn);

        /**
         * Gallery access to data
         **/
        $galleryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getGalleryDao();
        $galleryDao->setDbConnection($conn);

        /**
         * Media set access to data
         **/
        $setDao  = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getSetDao();
        $setDao->setDbConnection($conn);

        /**
         * Get set info
        **/
        $set = $setDao->getById($set_id);

        /**
        * Get file
        **/
        $file = $fileDao->getById($file_id);

        $fileUrl    = $file->url;
        $path    = str_replace($this->view->APP_STATIC_SERVER,getcwd(),$fileUrl);
        $pathinfo = pathinfo($path);
        $dirname = $pathinfo['dirname'];
        $filename = $pathinfo['filename'];
        $extension = $pathinfo['extension'];
        $unUrl = $dirname.'/'.$filename;

        $bool = $galleryDao->removeFromSet($file_id);
        if($bool){
            $arrType = array('_crop', '_large', '_medium', '_small', '_square', '_thumbnail');
            foreach($arrType as $row){
                $type = $row.'.'.$extension;
                unlink($unUrl.$type);
            }
            $fileDao->delete($file_id);

            /**
             * Get default image for set
            **/
            $photos = $fileDao->getFilesInSet($set_id, 0, 1, 1);


            /**
            * Update default image for set
            **/
            $updateSet = new Multimedia_Models_Set(array(
                'set_id'          => $set_id,
                'image_square'    => isset($photos[0]->image_square)?$photos[0]->image_square:null,
                'image_thumbnail' => isset($photos[0]->image_thumbnail)?$photos[0]->image_thumbnail:null,
                'image_small'     => isset($photos[0]->image_small)?$photos[0]->image_small:null,
                'image_crop'      => isset($photos[0]->image_crop)?$photos[0]->image_crop:null,
                'image_medium'    => isset($photos[0]->image_medium)?$photos[0]->image_medium:null,
                'image_large'     => isset($photos[0]->image_large)?$photos[0]->image_large:null,
            ));
            $galleryDao->updateSet($updateSet);

            /**
             * update number image form set
            **/
            $galleryDao->updateNImage($set_id, 1, '-');
        }

        $arrRouteAlbum = array(
            'enterprise_id' => $user->enterprise->id,
            'enterprise_name' => Tomato_Utility_String::removeSign($user->enterprise->company_name_en, '-', true),
            'slug' => $set->slug,
            'set_id' => $set->set_id
        );
        $this->_redirect($this->view->serverUrl() . $this->view->url($arrRouteAlbum, 'b2b_gallery_album'));
    }

    /**
     * Edit ablum info
     * @return void
    **/
    public function editAlbumAction(){

        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        $this->_helper->getHelper('layout')->disableLayout();
        $this->_helper->getHelper('viewRenderer')->setNoRender();

        $request = $this->getRequest();

        if($request->isPost()){

            /**
            * Get connection to database
            **/
            $conn = Tomato_Db_Connection::factory()->getMasterConnection();

            /**
            * Enterprise basic model access to db
            */
            $galleryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getGalleryDao();
            $galleryDao->setDbConnection($conn);

            $set_id = $request->getPost('set_id');
            $set_name = $request->getPost('set_title');

            $set = new Multimedia_Models_Set(array(
                'set_id' => $set_id,
                'title'  => $set_name,
                'slug'   => Tomato_Utility_String::removeSign($set_name, '-', true),
            ));

            $galleryDao->updateSetName($set);
        }
    }

    /**
     * Edit detail info
     * @return void
    **/
    public function editDetailAction(){

        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        $this->_helper->getHelper('layout')->disableLayout();
        $this->_helper->getHelper('viewRenderer')->setNoRender();

        $request = $this->getRequest();

        if($request->isPost()){

            /**
            * Get connection to database
            **/
            $conn = Tomato_Db_Connection::factory()->getMasterConnection();

            /**
            * File model access to db
            */
            $fileDao = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getFileDao();
            $fileDao->setDbConnection($conn);

            $file_id = $request->getPost('file_id');
            $file_name = $request->getPost('file_title');

            $fileDao->updateDescription($file_id, $file_name);
        }
    }

    public function deleteAlbumAction(){

        /**
        * Check not login yet
        **/
        $auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $user = $auth->getIdentity();
            $this->view->assign('user', $user);
        }
        else{
            $this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));
        }

        $request = $this->getRequest();

        /**
        * Get connection to database
        **/
        $conn = Tomato_Db_Connection::factory()->getMasterConnection();

        /**
        * File model access to db
        */
        $fileDao = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getFileDao();
        $fileDao->setDbConnection($conn);

        /**
        * Set model access to db
        */
        $setDao = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getSetDao();
        $setDao->setDbConnection($conn);

        /**
        * Gallery model access to db
        */
        $galleryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getGalleryDao();
        $galleryDao->setDbConnection($conn);

        $set_id = $request->getParam('set_id');
        $enterprise_id = $request->getParam('enterprise_id');

        /**
        * Get file in set
        **/
        $files = $fileDao->getFilesInSet($set_id, null, null, 1);
        if(count($files)){
            foreach($files as $file){
                $file_id = $file->file_id;
                $fileUrl    = $file->url;
                $path    = str_replace($this->view->APP_STATIC_SERVER,getcwd(),$fileUrl);
                $pathinfo = pathinfo($path);
                $dirname = $pathinfo['dirname'];
                $filename = $pathinfo['filename'];
                $extension = $pathinfo['extension'];
                $unUrl = $dirname.'/'.$filename;

                $bool = $galleryDao->removeFromSet($file_id);

                if($bool){
                    $arrType = array('_crop', '_large', '_medium', '_small', '_square', '_thumbnail');
                    foreach($arrType as $row){
                        $type = $row.'.'.$extension;
                        unlink($unUrl.$type);
                    }
                    $fileDao->delete($file_id);
                }
            }
        }

        /**
         * Delete set
        **/
        $setDao->delete($set_id);

        /**
         * Delete relationship enterprise and set
        **/
        $galleryDao->deleteAlbum($set_id);

        $arrRouteList = array(
            'enterprise_id' => $user->enterprise->id,
            'enterprise_name' => Tomato_Utility_String::removeSign($user->enterprise->company_name_en, '-', true),
        );
        $this->_redirect($this->view->serverUrl() . $this->view->url($arrRouteList, 'b2b_gallery_list'));
    }
    
    public function markAction()
    {
		$this->_helper->getHelper('layout')->disableLayout();
		$this->_helper->getHelper('viewRenderer')->setNoRender();
		
    	$request = $this->getRequest();
        $result  = 'RESULT_ERROR';
        if($request->isPost())
        {
	        $file_id = $request->getParam('file_id');
	        $type = $request->getParam('type');	        
	        
	        /**
	        * Check not login yet
	        **/
	        $auth = Zend_Auth::getInstance();
	        if ($auth->hasIdentity()) {
	            $user = $auth->getIdentity();
	            $this->view->assign('user', $user);
	        }
	        else{
	        	$this->_redirect($this->view->serverUrl() . $this->view->url(array(), 'bizprofile_auth_login'));            
	        }
	
	        /**
	         * Check user enterprise exist
	        **/
	        if(!isset($user->enterprise->id)){
	        	$allow_mark = 0;
	            die('Not foud enterprise');
	        }
	        
	        
	        /**
	        * Get connection to database
	        **/
	        $conn = Tomato_Db_Connection::factory()->getMasterConnection();
	
	        /**
	         * Media file access to data
	         **/
	        $fileDao = Tomato_Model_Dao_Factory::getInstance()->setModule('multimedia')->getFileDao();
	        $fileDao->setDbConnection($conn);
	        
	        /**
	         * Gallery access to data
	         **/
	        $galleryDao = Tomato_Model_Dao_Factory::getInstance()->setModule('b2b')->getGalleryDao();
	        $galleryDao->setDbConnection($conn);
	
	       	$checkmark = $galleryDao->checkmark($file_id);
	       	
	       	//$markValue = ($type == 'mark')?1:0;
	       	
	       	if($type == 'mark'){
	       		$void = $galleryDao->mark($file_id);
	       	}else{
	       		$void = $galleryDao->unmark($file_id);
	       	}
	        
	        $result = 'RESULT_OK';
	    }
        $this->getResponse()->setBody($result);
        
    }
    
    
}